package com.gitee.feizns.zxw.service.backstage.impl;

import com.gitee.feizns.web.domain.token.Token;
import com.gitee.feizns.web.ex.ServiceException;
import com.gitee.feizns.web.utils.EncryptUtils;
import com.gitee.feizns.web.utils.UserUtils;
import com.gitee.feizns.zxw.domain.backstage.ao.auth.login.LoginAo;
import com.gitee.feizns.zxw.domain.backstage.vo.auth.login.LoginVo;
import com.gitee.feizns.zxw.domain.error.BackstageCode;
import com.gitee.feizns.zxw.mapper.backstage.AuthMapper;
import com.gitee.feizns.zxw.service.backstage.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author feizns
 * @since 2019/9/9
 */
@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private AuthMapper authMapper;

    @Override
    public LoginVo login(LoginAo ao) {
        Integer id = authMapper.getUserIdByActAndPwd(ao.getAct(), EncryptUtils.base64Encode(ao.getPwd()));

        if ( id == null )
            throw new ServiceException(BackstageCode.USERNAME_OR_PASSWORD_IS_INCORRECT);

        Token token = UserUtils.generate(id);
        return new LoginVo(token.getValue(), token.getExpireTime());
    }

}
